package com.kehan.aidl_client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kehan.aidl_server.IRemoteAidlServiceInterface;
import com.kehan.aidl_server.entity.SensorConfig;

public class MainActivity extends AppCompatActivity {


    private TextView mTempText;
    private Button mGetButton;
    private Button mOpenButton;
    private Button mShutButton;

    private IRemoteAidlServiceInterface mSensorAidlInterface;
    private SensorServiceConnection mServiceConnection;

    private class SensorServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d("Yi","connection 中 iBinder的类："+iBinder.getClass().getSimpleName());
            //得到来自服务端的Binder
            mSensorAidlInterface = IRemoteAidlServiceInterface.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mSensorAidlInterface=null;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        bindService();
    }

    private void initView() {
        mTempText = findViewById(R.id.text_temp);
        mGetButton = findViewById(R.id.button_getTemp);
        mOpenButton = findViewById(R.id.button_open);
        mShutButton = findViewById(R.id.button_shut);

        mGetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    //跨进程通信，获取服务端的传感器温度值
                    double temperature = mSensorAidlInterface.getPid();
                    mTempText.setText("进程 id: "+temperature);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        mOpenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    SensorConfig sensorConfig = new SensorConfig();
                    sensorConfig.setmSensorName("TI Temperature Sensor");
                    sensorConfig.setmSensorCmd("telnet 10.100.31.14 9090");
                    //跨进程发送对象
                    mSensorAidlInterface.setSensor(sensorConfig);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        mShutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {

                    //跨进程发送对象
                    mSensorAidlInterface.basicTypes(1,2L,false,3f,4.8,"客户端");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void bindService() {
        //这是隐式启动Service的方法，Android5.0开始就不支持了
        //Intent intent = new Intent("com.pyjtlk.sensorapp.SensorService");
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.kehan.aidl_server", "com.kehan.aidl_server.service.MyService"));
        mServiceConnection = new SensorServiceConnection();
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(mServiceConnection);
    }
}